package com.huanuo.internetofthings.service.impact;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.huanuo.internetofthings.bo.deviceType.DeviceType;
import com.huanuo.internetofthings.bo.impact.ImpactDeviceDetailInfo;
import com.huanuo.internetofthings.bo.impact.ImpactDeviceInfo;
import com.huanuo.internetofthings.bo.jasperSimInfo.JasperSimInfo;
import com.huanuo.internetofthings.dto.impact.ImpactDeviceCityRes;
import com.huanuo.internetofthings.dto.impact.ImpactDeviceDetailInfoRes;
import com.huanuo.internetofthings.dto.impact.ImpactDeviceListInfoRes;
import com.huanuo.internetofthings.dto.statistics.DeviceInfoPageRequestDto;
import com.huanuo.internetofthings.service.deviceType.DeviceTypeService;
import com.huanuo.internetofthings.service.jasperSimInfo.JasperSimInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.unicorn.framework.base.base.AbstractService;
import org.unicorn.framework.core.SysCode;
import org.unicorn.framework.core.exception.PendingException;
import org.unicorn.framework.util.common.BeanMapping;

import java.util.List;

@Service
public class ImpactDeviceInfoFacadeService extends AbstractService {

	@Autowired
	private ImpactDeviceInfoService impactDeviceDeviceInfoService;
	
	@Autowired
	private ImpactDeviceDetailInfoService impactDeviceDetailInfoService;

	@Autowired
	private JasperSimInfoService jasperSimInfoService;
	
	@Autowired
	private DeviceTypeService deviceTypeService;
	
	/** 
	 * 分页查询设备信息
	 * 
	 * @param impactDeviceStatistics
	 * @return
	 * @throws PendingException
	 */
	public PageInfo<ImpactDeviceListInfoRes> getImpactDeviceInfoPage(DeviceInfoPageRequestDto deviceInfoPageRequestDto)
			throws PendingException {
		try {
			PageHelper.startPage(deviceInfoPageRequestDto.getPageNo(),deviceInfoPageRequestDto.getPageSize());
			List<ImpactDeviceInfo> list=impactDeviceDeviceInfoService.getImpactDeviceInfoList(deviceInfoPageRequestDto.getImpactDeviceInfo());
			PageInfo<ImpactDeviceInfo> impactDeviceInfoPage = new PageInfo<>(list);
			List<ImpactDeviceListInfoRes> resList=Lists.newArrayList();
			list.forEach(impactDeviceInfo->{
				ImpactDeviceListInfoRes impactDeviceListInfoRes=new ImpactDeviceListInfoRes();
				impactDeviceListInfoRes.setImpactDeviceInfo(impactDeviceInfo);
				//查询设备绑定sim卡信息
				try {
					JasperSimInfo jasperSimInfo = jasperSimInfoService.selectOne(JasperSimInfo.builder().iccid(impactDeviceInfo.getIccid()).build());
					impactDeviceListInfoRes.setJasperSimInfo(jasperSimInfo);
				} catch (PendingException e) {
					error("查询设备绑定的Sim卡信息失败",e);
				}
				resList.add(impactDeviceListInfoRes);
			});
			PageInfo<ImpactDeviceListInfoRes> pageInfo = BeanMapping.map(impactDeviceInfoPage, PageInfo.class);
			pageInfo.setList(resList);
			return pageInfo;
		} catch (PendingException pe) {
			throw  pe;
		}catch (Exception e) {
			error("分页查询设备信息失败", e);
			throw new PendingException(SysCode.SYS_FAIL, "分页查询设备信息失败");
		}
	}
	
	/**
	 * 查询设备详细信息
	 * 
	 * @param impactDeviceStatistics
	 * @return
	 * @throws PendingException
	 */
	@Cacheable(value="impactDeviceDetail",key="#impactDeviceInfo.id" ,sync = true)
	public ImpactDeviceDetailInfoRes deviceDetail(ImpactDeviceInfo impactDeviceInfo)throws PendingException {
		ImpactDeviceDetailInfoRes impactDeviceDetailInfoRes=new ImpactDeviceDetailInfoRes();
		//查询设备基础信息
		ImpactDeviceInfo queryImpactDeviceInfo=impactDeviceDeviceInfoService.getImpactDeviceInfo(impactDeviceInfo.getId());
		if(queryImpactDeviceInfo==null){
			throw new PendingException(SysCode.SYS_FAIL,"设备不存在");
		}
		//查询设备详情
		ImpactDeviceDetailInfo impactDeviceDetailInfo=impactDeviceDetailInfoService.getImpactDeviceDetailInfoMapper(ImpactDeviceDetailInfo.builder().deviceNo(queryImpactDeviceInfo.getDeviceNo()).build());
		//查询设备绑定sim卡信息
		JasperSimInfo jasperSimInfo=jasperSimInfoService.selectOne(JasperSimInfo.builder().devideId(queryImpactDeviceInfo.getIccid()).build());
		impactDeviceDetailInfoRes.setImpactDeviceInfo(queryImpactDeviceInfo);
		impactDeviceDetailInfoRes.setImpactDeviceDetailInfo(impactDeviceDetailInfo);
		impactDeviceDetailInfoRes.setJasperSimInfo(jasperSimInfo);
		return  impactDeviceDetailInfoRes;
	}
	
	
	/** 
	 * 用户查询职责范围内设备在某市分布列表
	 * 
	 * @param impactDeviceStatistics
	 * @return
	 * @throws PendingException
	 */
	public List<ImpactDeviceCityRes> getImpactDeviceInfoList(ImpactDeviceInfo impactDeviceInfo)
			throws PendingException {
		try {
			List<ImpactDeviceCityRes> relist=Lists.newArrayList();
			List<ImpactDeviceInfo> list=impactDeviceDeviceInfoService.getImpactDeviceInfoList(impactDeviceInfo);
			list.forEach(queryImpactDeviceInfo->{
				ImpactDeviceCityRes impactDeviceCityRes=new ImpactDeviceCityRes();
				impactDeviceCityRes.setImpactDeviceInfo(queryImpactDeviceInfo);
				//设置设备类型
				try {
					DeviceType deviceType=deviceTypeService.selectByPrimaryKey(queryImpactDeviceInfo.getDeviceTypeId());
					impactDeviceCityRes.setDeviceType(deviceType);
				} catch (PendingException e) {
					error("设置设备类型信息失败",e);
				}
				relist.add(impactDeviceCityRes);
			});
			return relist;
		} catch (PendingException pe) {
			throw  pe;
		}catch (Exception e) {
			error("用户查询职责范围内设备在某市分布列表", e);
			throw new PendingException(SysCode.SYS_FAIL, "用户查询职责范围内设备在某市分布列表");
		}
	}
	
	
	
}